package com.cljz.makepassenger.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.utils.T;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by apple on 2017/5/14.
 */

public class ChangeGroupNameActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.et_group_name)
    EditText etGroupName;
    @BindView(R.id.tv_group_name_num)
    TextView tvGroupNameNum;
    private String groupName;
    private String groupId;

    @Override
    public void initData(Bundle savedInstanceState) {
        groupName = getIntent().getExtras().getString("groupName");
        groupId = getIntent().getExtras().getString("groupId");
        tvTitleName.setText("修改群名称");
        flTitleRight.setVisibility(View.GONE);
        etGroupName.setText(groupName);
        etGroupName.setSelection(groupName.length());
        tvGroupNameNum.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) + "'>" + groupName.length() + "</font>/20"), TextView.BufferType.SPANNABLE);
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxTextView.textChanges(etGroupName)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 0) {
                            flTitleRight.setVisibility(View.GONE);
                        } else {
                            if (groupName.equals(charSequence.toString())) {
                                flTitleRight.setVisibility(View.GONE);
                            } else {
                                tvTitleRight.setText("完成");
                                flTitleRight.setVisibility(View.VISIBLE);
                            }
                        }
                        tvGroupNameNum.setText(Html.fromHtml("<font color='" + getResources().getColor(R.color.text_color) + "'>" + charSequence.length() + "</font>/20"), TextView.BufferType.SPANNABLE);
                    }
                });
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(ChangeGroupNameActivity.this);
                    }
                });
        RxView.clicks(flTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        changeGroupName();
                    }
                });
    }

    private void changeGroupName() {
        final String changedGroupName = etGroupName.getText().toString().trim();
        Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                try {
                    //修改群名称
                    EMClient.getInstance().groupManager().changeGroupName(groupId, changedGroupName);//需异步处理
                    subscriber.onNext(true);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    subscriber.onNext(false);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<Boolean>bindToLifecycle())
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {
                            T.getInstance().showShort("修改成功");
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putString("changedGroupName", changedGroupName);
                            intent.putExtras(bundle);
                            setResult(RESULT_OK, intent);
                            Router.pop(ChangeGroupNameActivity.this);
                        } else {
                            T.getInstance().showShort("修改失败");
                            setResult(RESULT_CANCELED);
                        }
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_group_name;
    }

    @Override
    public Object newP() {
        return null;
    }

}
